﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using MusicStore.Models;
using MusicStore.Repo;

namespace MusicStore.Dashboard.Service
{
  // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Sales" in code, svc and config file together.
  public class Sales : ISales
  {
    public IList<SalesByMonth> GetSalesByMonth()
    {
      Thread.Sleep(500);

      var wMusicStoreRepoEntities = new MusicStoreRepoEntities();
      IList<Sale> wSales = wMusicStoreRepoEntities.GetSales();

      var wSalesByMonth = from sale in wSales
                          group sale by sale.SellDate.Month
                          into saleByMonth
                          select new SalesByMonth() { Month = IntToMonth(saleByMonth.Key), Amount = saleByMonth.Sum(s => s.Amount)};

      if (wSalesByMonth.Count() == 0) return new List<SalesByMonth>() {new SalesByMonth() {Month = "No sales", Amount = 100}};
      return wSalesByMonth.ToList();

      return new List<SalesByMonth>()
      {
        new SalesByMonth() {Month = "Jan", Amount = 34.5m},
        new SalesByMonth() {Month = "Fev", Amount = 12.58m},
        new SalesByMonth() {Month = "Mar", Amount = 3.5m},
        new SalesByMonth() {Month = "Apr", Amount = 28.3m}
      };
    }

    private static string IntToMonth(int monthNumber)
    {
      string[] months = new string[]
      {
        "Janvier",
        "Février",
        "Mars",
        "Avril",
        "Mai",
        "Juin",
        "Juillet",
        "Août",
        "Septembre",
        "Octobre",
        "Novembre",
        "Décembre"
      };

      return months[monthNumber - 1];
    }

    public IList<SalesByGenre> GetSalesByGenre()
    {
      Thread.Sleep(1500);

      return new BindingList<SalesByGenre>()
      {
        new SalesByGenre() {Genre = "Rock", Amount = (98.4m)},
        new SalesByGenre() {Genre = "Rap", Amount = (26.2m)},
        new SalesByGenre() {Genre = "Jazz", Amount = (67.4m)}
      };
    }
  }
}